package app.util.image.action.morph
{
	import app.util.*;
	import app.util.image.*;
	import app.util.image.action.*;
	import app.util.image.calc.*;
	
	import flash.display.*;

	public class SkeletonAction extends SEBasedAction
	{
		private var src:BinaryBitmapData;
		private var mid:BinaryBitmapData;
		private var ret:BinaryBitmapData;		
		private var onlyMode:Boolean;
		private var progress:Number = 0;
		
		public function SkeletonAction(only:Boolean)
		{
			super(1);
			this.onlyMode = only;
		}
		
		public override function getDescription():String
		{
			return this.onlyMode ? "Skeleton" : "Skeleton With Original";
		}
		
		protected override function getFinalBitmapData():BitmapData
		{
			if(this.onlyMode)
			{
				return this.ret.toBitmapData();
			}
			else
			{
				return MorphUtility.autoThreshold(Mediator.getCurrentBitmapData()).addWithColor(this.ret, 0xff0000);
			}
		}
		
		protected override function processBitmapDataByStep():Number
		{
			if(this.ret == null)
			{
				se = MorphUtility.getDefaultSE();
				src = MorphUtility.autoThreshold(Mediator.getCurrentBitmapData());	
				ret = new BinaryBitmapData(src.getWidth(), src.getHeight());
				mid = MorphUtility.doErosion(src, se);
			}
				
			if(!mid.isEmpty())
			{
				var tmp:BinaryBitmapData = MorphUtility.doDilation(MorphUtility.doErosion(mid, se), se);
				var diff:BinaryBitmapData = mid.minus(tmp);
				ret = ret.add(diff);
				src = mid;
				mid = MorphUtility.doErosion(src, se);
				
				progress += 0.03;
				if(progress < 1)
				{
					return progress;
				}
				else
				{
					return 0.95;
				}
			}			
			else
			{
				return 1.0;
			}
		}
	}
}